package mo.leetcode_hot;

import java.util.ArrayList;
import java.util.List;

public class L46 {

    public List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        if (len == 0) {
            return res;
        }
        List<Integer> combination = new ArrayList<>();
        boolean[] used = new boolean[len];
        dfs(len, res, combination, nums, used, 0);
        return res;
    }

    private void dfs(int n, List<List<Integer>> res, List<Integer> combination, int[] nums, boolean[] used, int depth) {
        if (depth == n) {
            res.add(new ArrayList<>(combination));
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!used[i]) {
                combination.add(nums[i]);
                used[i] = true;
                dfs(n, res, combination, nums, used, depth+1);
                used[i] = false;
                combination.remove(combination.size()-1);
            }
        }
    }

}
